package com.dries.portal.module.wechat.wxma.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.dries.portal.module.wechat.wxma.config.WxMaConfig;
import com.dries.portal.module.wechat.wxma.service.IWeChatService;
import com.dries.portal.module.wechat.wxma.vo.OrderPayMsgVo;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/10/28 11:26
 */
@Service
public class WeChatServiceImpl implements IWeChatService {

    /**
     * 发送支付通知 订阅消息
     *
     * @param msgVo
     */
    @Override
    public void orderPayWxMsgSender(OrderPayMsgVo msgVo) {

//        WxMaUniformMessage uniformMessage = new WxMaUniformMessage();
//
//        // 公众号appid，要求与小程序有绑定且同主体.
//        uniformMessage.setAppid("wxef55485ca33f2891");
//        // 是否发送公众号模板消息
//        uniformMessage.setMpTemplateMsg(true);
//        /**
//         * 用户openid.
//         * 可以是小程序的openid，也可以是mp_template_msg.appid对应的公众号的openid
//         */
//        uniformMessage.setToUser(msgVo.getUserId());
//        // 公众号或小程序模板ID.
//        uniformMessage.setTemplateId();
//        // 小程序模板消息formid.
//        uniformMessage.setFormId();
//        // 公众号模板消息所要跳转的小程序，小程序的必须与公众号具有绑定关系.
//        WxMaUniformMessage.MiniProgram miniProgram = new WxMaUniformMessage.MiniProgram();
//        miniProgram.setAppid("wxb9db293a758196bd");
//        miniProgram.setPagePath("index");
//        // miniProgram.setUsePath();
//        uniformMessage.setMiniProgram(miniProgram);
//        // 公众号模板消息所要跳转的url.
//        uniformMessage.setUrl("www.baidu.com");
//        // 小程序页面路径.
//        uniformMessage.setPage("index");
//        uniformMessage.addData(new WxMaTemplateData());

        /**
         * 用户userId
         *
         * 商家名称 thing9
         * 支付金额 amount3
         * 订单号 character_string7
         * 订单状态 phrase6
         * 温馨提示 thing1
         */
        final WxMaService wxMaService = WxMaConfig.getMaService("wxb9db293a758196bd");
        WxMaSubscribeMessage message = new WxMaSubscribeMessage();
        message.setTemplateId("71R1DPME1wipvsfDwdR4F-4ZeuLDi65XBKHjrAtuwLo");
        message.setToUser(msgVo.getOpenId());
        message.setPage("page/index");
        message.setLang(WxMaConstants.MiniprogramLang.ZH_CN);
        message.setMiniprogramState(WxMaConstants.MiniprogramState.FORMAL);
        message.addData(new WxMaSubscribeMessage.Data("thing9", msgVo.getThing9()));
        message.addData(new WxMaSubscribeMessage.Data("amount3", msgVo.getAmount3()));
        message.addData(new WxMaSubscribeMessage.Data("character_string7", msgVo.getCharacter_string7()));
        message.addData(new WxMaSubscribeMessage.Data("phrase6", msgVo.getPhrase6()));
        message.addData(new WxMaSubscribeMessage.Data("thing1", msgVo.getThing1()));

        try {
            wxMaService.getMsgService().sendSubscribeMsg(message);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

    }
}
